package com.zx.erp.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.erp.sys.bean.Module;
import com.zx.erp.sys.service.ModuleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者信息
 * @since 2021-11-10
 */
@Controller
@RequestMapping("/sys/modules")
@SessionAttributes(value = {"parentModule"})
public class ModuleController {

    @Autowired
    private ModuleService moduleService;

    @RequestMapping("/list")
    public String list(Integer jobId , Model model, Module module, @RequestParam(value = "pageno", defaultValue = "1") Integer
            pageNo){
        Integer pageSize = 6;
        Page<Module> pageInfo = moduleService.pageQuery(module, pageNo, pageSize);
        List<Module> allParentModule = moduleService.findAllParentModuleNoParameter();
        model.addAttribute("parentModule",allParentModule);
        model.addAttribute("mp",pageInfo);
        return "forward:/sys/modules/moduleList.html";
    }

    @RequestMapping("/doAdd")
    public String doAdd(Module module){
        boolean b = moduleService.addNewModule(module);
        return "redirect:/sys/modules/list";
    }

    @RequestMapping("/details/{moduleId}")
    public String details(@PathVariable("moduleId") Integer moduleId,Model model){
        Module module = moduleService.selectOneModule(moduleId);
        model.addAttribute("module",module);
        return "forward:/sys/modules/moduleView.html";
    }

    @RequestMapping("/toUpdate/{moduleId}")
    public String toUpdate(@PathVariable("moduleId") Integer moduleId,Model model){
        Module module = moduleService.selectOneModule(moduleId);
        model.addAttribute("byId",module);
        return "forward:/sys/modules/moduleUpdate.html";
    }

    @RequestMapping("/doUpdate")
    public String doUpdate(Module module){
        boolean b = moduleService.doUpdate(module);
        return "redirect:/sys/modules/list";
    }

    @RequestMapping("/doState/{moduleId}/{state}")
    public String doState(@PathVariable("moduleId") Integer moduleId,@PathVariable("state")Integer moduleState){
        if(moduleState==1){
            moduleState = 2;
        }else {
            moduleState = 1;
        }
        boolean b = moduleService.doState(moduleId,moduleState);
        return "redirect:/sys/modules/list";
    }

}

